@extends('admin.layout.base')
@section('styles')
    <style>
        .el-table__expand-icon {
            position: relative;
            width: 50px;
        }
    </style>
@endsection
@section('content')
    <div class="panel-content" id="app" v-cloak>
        <div class="panel-body panel-list">
            <div class="panel-header">
                <div class="panel-title">
                    站内消息
                </div>
            </div>
            <div class="panel-main">
                <div class="panel-btns">
                    <el-button type="success" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                </div>
                <div class="panel-search">
                    <div class="panel-search-item">
                        <el-input  size="medium" v-model="params.kwd" clearable placeholder="请输入消息标题" class="input-with-select">
                            <el-button type="primary" @click="search()" slot="append" icon="el-icon-search"></el-button>
                        </el-input>
                    </div>
                </div>
                <el-table v-loading="loading"
                          element-loading-text="努力加载中..."
                          element-loading-spinner="el-icon-loading"
                          ref="multipleHandle"
                          tooltip-effect="dark"
                          :data="listData.data">
                    <el-table-column align="center" type="expand" width="50">
                        <template slot-scope="scope">
                            @{{ scope.row.content }}
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                    <el-table-column label="标题" prop="title" :show-overflow-tooltip="true" width="350"></el-table-column>
                    <el-table-column label="用户" prop="user.username" width="150"></el-table-column>
                    <el-table-column align="center" label="类别" width="100">
                        <template slot-scope="scope">
                            <span v-html="scope.row.type_text"></span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="状态" width="100">
                        <template slot-scope="scope">
                            <span v-html="scope.row.status_text"></span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="created_at" label="发布日期" width="165"></el-table-column>
                    <el-table-column label="操作" >
                        <template slot-scope="scope">
                            <el-button @click="destroy(scope.row.id)" icon="fa fa-trash" type="info" size="mini" plain> 删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination"  v-if="listData && listData.total">
                    <div class="pagination-total">共 <em> @{{listData.total}} </em> 条记录</div>
                    <el-pagination v-if="listData.last_page > 1"
                                   background
                                   :page-size="listData.per_page"
                                   layout="prev, pager, next, jumper"
                                   :total="listData.total"
                                   prev-text="上一页"
                                   next-text="下一页"
                                   :current-page="listData.current_page"
                                   @current-change="changePage">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('scripts')
    <script>
        let app = new Vue({
            el: '#app',
            data: function() {
                return {
                    listData: [],//列表数据
                    params: {page: 1, kwd: ''},
                    loading: false,
                }
            },
            created(){
                this.list();
            },
            methods: {
                //分页
                changePage(val) {
                    this.params.page = val;
                    this.list();
                },
                //列表
                list(){
                    let that = this;
                    this.loading = true;
                    setTimeout(()=>{
                        $.get("{{ route('admin.mail.list') }}",this.params,function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.listData = res.data;
                                that.loading = false;
                            }
                        },'JSON');
                    },500);
                },
                //筛选
                search(){
                    this.params.page = 1;
                    this.list();
                },
                //删除
                destroy(id){
                    let that = this;
                    this.$confirm('确定要删除选中的消息吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        $.post("{{ route('admin.mail.destroy') }}",{id: id, _token: "{{csrf_token()}}"},function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.list();
                            }
                        },'JSON');
                    }).catch(() => {});
                },
            }
        });
    </script>
@endsection
